package com.ordobill.webapp.action;

import javax.servlet.http.*;

import org.apache.struts.action.*;
import org.apache.commons.logging.*;
import com.ibatis.sqlmap.client.*;
import com.ordobill.webapp.beans.Member;
import com.ordobill.webapp.common.Constants;
import com.ordobill.webapp.common.SqlMapClientManager;
import org.apache.struts.actions.*;

public class LoginAction extends DispatchAction{
	
	private Log log = LogFactory.getFactory().getInstance(this.getClass().getName());
	private SqlMapClient sqlMap = SqlMapClientManager.getSqlMapClient();

	public ActionForward login(ActionMapping mapping,
			                   ActionForm form,
			                   HttpServletRequest request,
			                   HttpServletResponse response) throws Exception {		
		Member memberForm = (Member) form; 
		Member memberData = (Member) sqlMap.queryForObject("Member.list", memberForm);
		ActionMessages errors = new ActionMessages();
		
		// 유저 유무 확인
		log.info("User I.D. Checking...");
		if (memberData == null) {
			errors.add("error", new ActionMessage("아이디가 일치하지 않습니다."));
			saveMessages(request, errors);
			log.debug("Invalidated I.D Please Try Again");
			return mapping.getInputForward();
		}
		
		log.debug("user data id : " + memberData.getMemId());
		log.debug("user form id : " + memberForm.getMemId());
		
		log.info("User Password Checking...");
		if(!memberData.isCheckoutPassword(memberForm.getMemPw())){
			log.debug("Invalidated Password Please Try Again");
			errors.add("error", new ActionMessage("비밀번호가 일치하지 않습니다."));
			saveMessages(request, errors);
			return mapping.getInputForward();
		}
		// 승인 체크
		log.info("User approval Checking...");
		if("N".equals(memberData.getMemOk())){
			log.debug("ask for[request] somebody's approval");
			errors.add("error", new ActionMessage("관리자 승인이 필요합니다."));
			saveMessages(request, errors);
			return mapping.getInputForward();
		}
		
		log.debug("user data password : " + memberData.getMemPw());
		log.debug("user form password : " + memberForm.getMemPw());	
		
		log.info("Now is Session Setting...");
		
		HttpSession session = request.getSession();
		
		memberData.setMemPw(null);
		session.setAttribute(Constants.SESSION_UNIT, memberData);
		session.setAttribute(Constants.SESSION_UNIT_UID, ""+memberData.getMemUid());
		session.setAttribute(Constants.SESSION_UNIT_NAME, memberData.getMemName());
		session.setAttribute(Constants.SESSION_UNIT_ID, memberData.getMemId());
		session.setAttribute(Constants.SESSION_UNIT_COMPANY, memberData.getMemCompany());
		session.setAttribute(Constants.SESSION_UNIT_EMAIL, memberData.getMemEmail());
		session.setAttribute(Constants.SESSION_UNIT_TEL, memberData.getMemTel());
		session.setAttribute(Constants.SESSION_UNIT_ADDR, memberData.getMemAddress());
		session.setAttribute(Constants.SESSION_UNIT_LEVEl, memberData.getMemLevel());
		session.setAttribute(Constants.SESSION_UNIT_OK, memberData.getMemOk());
		session.setAttribute(Constants.SESSION_GETID, session.getId());
		
		return mapping.findForward("success"); 
	}
	
	public ActionForward logout(ActionMapping mapping,
			                    ActionForm form,
			                    HttpServletRequest request,
			                    HttpServletResponse response) throws Exception {

        // 로그 아웃 정보 입력
        log.info("Login out history...");
        
        ActionMessages messages = new ActionMessages();
        
        HttpSession session = request.getSession(false);
        if(!ProjectAction.simulationArr.isEmpty()){
            ProjectAction.simulationArr.remove((String)session.getAttribute("sessionGetId"));
        }
        session.invalidate();
        session = null;
        
        messages.add("message", new ActionMessage("로그아웃 되었습니다."));
        saveMessages(request, messages);
        
        return mapping.findForward("logout");						  	
    }	
}
